package com.lzx.iteam.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.lzx.iteam.AttendanceCalendarActivity;
import com.lzx.iteam.AttendanceCountActivity;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.AttendanceCountData;
import com.lzx.iteam.util.AllDialogUtil;
import com.lzx.iteam.util.StringUtil;

import java.util.ArrayList;


/**
 * Created by Megan on 16/4/8.
 */
public class AttendanceCountAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<AttendanceCountData> attendanceCountDatas;
    private String mAttendanceFormId;
    private String mGroupId;
    private AllDialogUtil mDialogUtil;

    public AttendanceCountAdapter(Context mContext, String groupId, String attendanceFormId) {
        this.mContext = mContext;
        this.mAttendanceFormId = attendanceFormId;
        this.mGroupId = groupId;
        mDialogUtil = AllDialogUtil.getInstance((AttendanceCountActivity) mContext);
    }


    public void bindData(ArrayList<AttendanceCountData> attendanceCountDatas) {
        this.attendanceCountDatas = attendanceCountDatas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return attendanceCountDatas == null ? 0 : attendanceCountDatas.size();
    }

    @Override
    public Object getItem(int i) {
        return attendanceCountDatas == null ? null : attendanceCountDatas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        final AttendanceCountData attendanceCountData = attendanceCountDatas.get(i);
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_attendance_count, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.tv_attendance_count_name);
            holder.nameLayout = (LinearLayout) convertView.findViewById(R.id.ll_attendance_count_name);
            holder.count1 = (TextView) convertView.findViewById(R.id.tv_attendance_count_onguard);
            holder.count2 = (TextView) convertView.findViewById(R.id.tv_attendance_count_business);
            holder.count3 = (TextView) convertView.findViewById(R.id.tv_attendance_count_vacation);
            holder.count4 = (TextView) convertView.findViewById(R.id.tv_attendance_count_goout);
            holder.countLegWork = (TextView) convertView.findViewById(R.id.tv_attendance_count_legwork);
            holder.count5 = (TextView) convertView.findViewById(R.id.tv_attendance_count_belate);
            holder.count6 = (TextView) convertView.findViewById(R.id.tv_attendance_count_early);
            holder.countUnWork = (TextView) convertView.findViewById(R.id.tv_attendance_count_un_work);
            holder.countAll = (TextView) convertView.findViewById(R.id.tv_attendance_count_all);
            holder.vacationCount = (LinearLayout) convertView.findViewById(R.id.ll_attendance_count_vacation);
            holder.root = (LinearLayout) convertView.findViewById(R.id.attendance_count_item_root);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (i % 2 == 0) {
            holder.root.setBackgroundColor(mContext.getResources().getColor(R.color.schedule_bg));
        } else {
            holder.root.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        }
        holder.name.setText(attendanceCountData.getUserName());

        if ("0".equals(attendanceCountData.getWorkDay())) {
            holder.count1.setText("0");
        } else {
            holder.count1.setText(attendanceCountData.getWorkDay());
        }
        holder.count1.setTextColor(mContext.getResources().getColor(R.color.schedule_content));

        if ("0".equals(attendanceCountData.getBusinessDay())) {
            holder.count2.setText("0");
            holder.count2.setTextColor(mContext.getResources().getColor(R.color.schedule_content));
        } else {
            holder.count2.setTextColor(mContext.getResources().getColor(R.color.schedule_content));
            holder.count2.setText(attendanceCountData.getBusinessDay());
        }

        if ("0".equals(attendanceCountData.getLeaveDay())) {
            holder.count3.setText("0");
            holder.count3.setTextColor(mContext.getResources().getColor(R.color.schedule_content));
        } else {
            holder.count3.setTextColor(mContext.getResources().getColor(R.color.attendance_count_blue1));
            holder.count3.setText(attendanceCountData.getLeaveDay());
        }

        if ("0".equals(attendanceCountData.getOutDay())) {
            holder.count4.setText("0");
            holder.count4.setTextColor(mContext.getResources().getColor(R.color.schedule_content));
        } else {
            holder.count4.setTextColor(mContext.getResources().getColor(R.color.schedule_content));
            holder.count4.setText(attendanceCountData.getOutDay());
        }
        if ("0".equals(attendanceCountData.getLegworkCount())) {
            holder.countLegWork.setText("0");
            holder.countLegWork.setTextColor(mContext.getResources().getColor(R.color.schedule_content));
        } else {
            holder.countLegWork.setTextColor(mContext.getResources().getColor(R.color.schedule_content));
            holder.countLegWork.setText(attendanceCountData.getLegworkCount());
        }

        if ("0".equals(attendanceCountData.getWorkBeLateCount())) {
            holder.count5.setText("0");
            holder.count5.setTextColor(mContext.getResources().getColor(R.color.schedule_content));
        } else {
            holder.count5.setTextColor(mContext.getResources().getColor(R.color.attendance_count_red));
            holder.count5.setText(attendanceCountData.getWorkBeLateCount());
        }
        if ("0".equals(attendanceCountData.getWorkLeaveEarlyCount())) {
            holder.count6.setText("0");
            holder.count6.setTextColor(mContext.getResources().getColor(R.color.schedule_content));
        } else {
            holder.count6.setTextColor(mContext.getResources().getColor(R.color.attendance_count_red));
            holder.count6.setText(attendanceCountData.getWorkLeaveEarlyCount());
        }
        if ("0".equals(attendanceCountData.getUnWorkDay())) {
            holder.countUnWork.setText("0");
            holder.countUnWork.setTextColor(mContext.getResources().getColor(R.color.schedule_content));
        } else {
            holder.countUnWork.setTextColor(mContext.getResources().getColor(R.color.attendance_count_red));
            holder.countUnWork.setText(attendanceCountData.getUnWorkDay());
        }

        if ("0".equals(attendanceCountData.getTotal())) {
            holder.countAll.setText("0");
        } else {
            holder.countAll.setText(attendanceCountData.getTotal());
        }
        holder.countAll.setTextColor(mContext.getResources().getColor(R.color.schedule_content));


        holder.vacationCount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                StringBuilder sb = new StringBuilder();
                if (!"0".equals(attendanceCountData.getLeaveDay())) {
                    if (!"0".equals(attendanceCountData.getLeaveType0())) {
                        sb.append("事假" + attendanceCountData.getLeaveType0() + "天\n");
                    }
                    if (!"0".equals(attendanceCountData.getLeaveType1())) {
                        sb.append("病假" + attendanceCountData.getLeaveType1() + "天\n");
                    }
                    if (!"0".equals(attendanceCountData.getLeaveType2())) {
                        sb.append("年假" + attendanceCountData.getLeaveType2() + "天\n");
                    }
                    if (!"0".equals(attendanceCountData.getLeaveType3())) {
                        sb.append("婚假" + attendanceCountData.getLeaveType3() + "天\n");
                    }
                    if (!"0".equals(attendanceCountData.getLeaveType4())) {
                        sb.append("产假" + attendanceCountData.getLeaveType4() + "天/n");
                    }
                    showVacationDay(sb.toString());
                }
            }
        });

        holder.nameLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((AttendanceCountActivity)mContext).setGuideGone();
                if (!StringUtil.isEmpty(attendanceCountData.getUserId()) && !"0".equals(attendanceCountData.getUserId())) {
                    /*Intent intent = new Intent(mContext, AttendanceCalendarActivity.class);
                    intent.putExtra("userId", attendanceCountData.getUserId());
                    intent.putExtra("userName", attendanceCountData.getUserName());
                    intent.putExtra("group_id", mGroupId);
                    intent.putExtra("form_id", mAttendanceFormId);
                    mContext.startActivity(intent);
                    */
                    ((AttendanceCountActivity) mContext).toAttendanceCalendarActivity(attendanceCountData);

                }else{
                    Toast.makeText(mContext,"对不起,该成员未注册,无法查看他的考勤记录",Toast.LENGTH_LONG).show();

                }
            }
        });

        return convertView;
    }

    class ViewHolder {
        TextView name, count1, count2, count3, count4, countLegWork,count5, count6, countUnWork,countAll;
        LinearLayout nameLayout;
        LinearLayout vacationCount;
        LinearLayout root;
    }


    private void showVacationDay(String msg) {
        mDialogUtil.dialogOneBtnStyle(msg, "确定");
        mDialogUtil.setOnDialogListener(new AllDialogUtil.DialogOnClickListener() {
            @Override
            public void onConfirmClick(Dialog lDialog) {
                lDialog.dismiss();
            }

            @Override
            public void onCancelClick(Dialog lDialog) {
                lDialog.dismiss();
            }
        });
    }
}
